package com.mobileagri.backoffice.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mobileagri.backoffice.model.Log;
import com.mobileagri.backoffice.service.LogService;
import com.mobileagri.backoffice.util.ExtJSReturn;

/**
 * Controller - Spring
 * 
 * 
 * @author LOUVET Romain
 */
@Controller
public class LogController {

	private LogService logService;

	// @RequestMapping(value = "/contact/view.action")
	// public @ResponseBody
	// Map<String, ? extends Object> view(@RequestParam int start,
	// @RequestParam int limit) throws Exception {
	//
	// try {
	//
	// List<Contact> contacts = contactService
	// .getContactList(start, limit);
	//
	// int total = contactService.getTotalContacts();
	//
	// return ExtJSReturn.mapOK(contacts, total);
	//
	// } catch (Exception e) {
	//
	// return ExtJSReturn
	// .mapError("Error retrieving Contacts from database.");
	// }
	// }

	@RequestMapping(value = "log/view2", method = RequestMethod.GET)
	public @ResponseBody
	Map<String, ? extends Object> view2(@RequestParam long _dc,
			@RequestParam int page, @RequestParam int start,
			@RequestParam int limit, @RequestParam String callback)
			throws Exception {

		System.out.println("list Logs");

		try {

			List<Log> logs = logService.getLogList();

			List<Log> logsNew = new ArrayList<Log>();
			for (int i = 0; i < 10; i++) {
				logsNew.add(logs.get(i));
			}

			// contactService.getContactList(start, limit)
			//

			int total = logService.getTotalLog();

			return ExtJSReturn.mapOK(logsNew, logsNew.size(), callback);

		} catch (Exception e) {

			return ExtJSReturn.mapError("Error retrieving Log from database."
					+ e.getMessage());
		}
	}

	// @RequestMapping(value = "/contact/create.action")
	// public @ResponseBody
	// Map<String, ? extends Object> create(@RequestBody Contact data)
	// throws Exception {
	//
	// try {
	//
	// List<Contact> contacts = contactService.create(data);
	//
	// return ExtJSReturn.mapOK(contacts);
	//
	// } catch (Exception e) {
	//
	// return ExtJSReturn.mapError("Error trying to create contact.");
	// }
	// }
	//
	// @RequestMapping(value = "/contact/update.action")
	// public @ResponseBody
	// Map<String, ? extends Object> update(@RequestBody ContactWrapper data)
	// throws Exception {
	// try {
	//
	// List<Contact> contacts = contactService.update(data.getData());
	//
	// return ExtJSReturn.mapOK(contacts);
	//
	// } catch (Exception e) {
	//
	// return ExtJSReturn.mapError("Error trying to update contact.");
	// }
	// }
	//
	// @RequestMapping(value = "/contact/delete.action")
	// public @ResponseBody
	// Map<String, ? extends Object> delete(@RequestBody ContactWrapper data)
	// throws Exception {
	//
	// try {
	//
	// contactService.delete(data.getData());
	//
	// Map<String, Object> modelMap = new HashMap<String, Object>(3);
	// modelMap.put("success", true);
	//
	// return modelMap;
	//
	// } catch (Exception e) {
	//
	// return ExtJSReturn.mapError("Error trying to delete contact.");
	// }
	// }

	@Autowired
	public void setLogService(LogService logService) {
		this.logService = logService;
	}

}
